﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;
using Word = Microsoft.Office.Interop.Word;
using Office = Microsoft.Office.Core;
using Microsoft.Office.Tools.Word;
using Microsoft.Office.Tools.Word.Extensions;
using Vsto = Microsoft.Office.Tools;
using System.IO;
using System.Collections;
using System.Drawing;
using System.Windows.Forms;

namespace WordAddIn1
{
    public partial class ThisAddIn
    {
        public  Vsto.CustomTaskPane MyBlogPane;
        private void ThisAddIn_Startup(object sender, System.EventArgs e)
        {
            MyBlogPane = this.CustomTaskPanes.Add(new MyBlogsUserControl(), "My blogs");
            MyBlogPane.VisibleChanged += new EventHandler(MyBlogPane_VisibleChanged);
            MyBlogPane.Visible = false;
        }

        void MyBlogPane_VisibleChanged(object sender, EventArgs e)
        {
            Ribbon1 myRibbon = (Ribbon1)Globals.Ribbons.Single(rb => rb.Name == "Ribbon1");
            myRibbon.MyBlogsBtn.Checked = MyBlogPane.Visible;
        }

        private void ThisAddIn_Shutdown(object sender, System.EventArgs e)
        {
        }


        public void ApplyTemplete(string tempSrc)
        {
            Globals.ThisAddIn.Application.ActiveDocument.ApplyDocumentTheme(tempSrc);
        }



        public void InsertPic(string  picSrc)
        {
            object linkToFile = false;
            object saveWithDoc = true;
            object left = 10;
            object top = 10;
            object width = 200;
            object heigth = 200;
            object anchor = System.Reflection.Missing.Value;
            Globals.ThisAddIn.Application.ActiveDocument.Shapes.AddPicture(picSrc, ref linkToFile, ref saveWithDoc, ref left, ref top, ref width, ref heigth, ref anchor);
        }

        public void ExportPic(string picSrc)
        {
            try
            {
                Globals.ThisAddIn.Application.Selection.Copy();
            }catch (Exception ex)
            {
                MessageBox.Show("请先选中图片！", "");
                return;
            }
            Image image = Clipboard.GetImage();
            if (image == null)
            {
                MessageBox.Show("请先选中图片！","");
                return;
            }
            Bitmap bitmap = new Bitmap(image);
            bitmap.Save(picSrc);

        }

        public void ExportTxt(string txtSrc)
        {
            try
            {
                Globals.ThisAddIn.Application.Selection.Copy();
            }
            catch (Exception ex)
            {
                MessageBox.Show("请先选中文本！", "");
                return;
            }
            string txt = Clipboard.GetText();
            if (txt == null)
            {
                MessageBox.Show("请先选中文本！", "");
                return;
            }
     

            FileStream filest = new FileStream(txtSrc, FileMode.OpenOrCreate, FileAccess.Write);
            StreamWriter sw = new StreamWriter(filest);
            sw.Write(txt);
            sw.Close();
        }

        public void InsertTxt(string txtFileSrc)
        {
             

            string myReadeString;
            StreamReader myStreamReader = File.OpenText(txtFileSrc);//myfilename就是你的文件名称了
            myReadeString = myStreamReader.ReadLine();
            while (myReadeString!=null)
            {
                Microsoft.Office.Interop.Word.Range WordRange = Globals.ThisAddIn.Application.Selection.Range;
                WordRange.Text = myReadeString+"\r\n";
                Globals.ThisAddIn.Application.Selection.Start +=( myReadeString.Length+1);
                myReadeString = myStreamReader.ReadLine();
            }
            myStreamReader.Close();

        }

        #region VSTO 生成的代码

        /// <summary>
        /// 设计器支持所需的方法 - 不要
        /// 使用代码编辑器修改此方法的内容。
        /// </summary>
        private void InternalStartup()
        {
            this.Startup += new System.EventHandler(ThisAddIn_Startup);
            this.Shutdown += new System.EventHandler(ThisAddIn_Shutdown);
        }
        
        #endregion
    }
}
